package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailServiceCenterDTO;
import com.dodoke.entity.RetailServiceCenter;
import com.dodoke.service.IRetailCompanyService;
import com.dodoke.service.IRetailRouteService;
import com.dodoke.service.IRetailServiceCenterService;
import com.dodoke.service.IRetailWarehouseService;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import com.dodoke.vo.RetailServiceCenterDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 服务中心控制层
 */
@Slf4j
@RestController
@RequestMapping("/retail/serviceCenter")
public class RetailServiceCenterController {
    @Autowired
    private IRetailServiceCenterService retailServiceCenterService;
    @Autowired
    private IRetailCompanyService retailCompanyService;
    @Autowired
    private IRetailWarehouseService retailWarehouseService;
    @Autowired
    private IRetailRouteService retailRouteService;

    /**
     * 服务中心信息分页查询
     *
     * @param currentPage            当前页码
     * @param pageSize               每页显示条数
     * @param retailServiceCenterDTO 模糊查询参数
     * @return
     */
    @PostMapping("getListByPage/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody RetailServiceCenterDTO retailServiceCenterDTO) {
        return AjaxResult.success("获取服务中心分页数据成功", retailServiceCenterService.getListByPage(currentPage, pageSize, retailServiceCenterDTO));
    }

    /**
     * 新增服务中心信息
     *
     * @param retailServiceCenter 服务中心信息参数
     * @return
     */
    @PostMapping
    public AjaxResult addRetailServiceCenter(@Validated @RequestBody RetailServiceCenter retailServiceCenter) {
        // 判断服务中心名称是否存在
        RetailServiceCenter serviceCenterByName = retailServiceCenterService.getOne(new LambdaQueryWrapper<RetailServiceCenter>().eq(RetailServiceCenter::getName, retailServiceCenter.getName()));
        if (serviceCenterByName != null) {
            return AjaxResult.error(HttpStatus.ERROR, "服务中心名称已存在");
        }
        // 判断服务中心编号是否存在
        if (retailServiceCenter.getCode() != null) {
            RetailServiceCenter serviceCenterByCode = retailServiceCenterService.getOne(new LambdaQueryWrapper<RetailServiceCenter>().eq(RetailServiceCenter::getCode, retailServiceCenter.getCode()));
            if (serviceCenterByCode != null) {
                return AjaxResult.error(HttpStatus.ERROR, "服务中心编号已存在");
            }
        }
        int resCount = retailServiceCenterService.saveRetailServiceCenter(retailServiceCenter);
        if (resCount <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "仓库信息服务中心添加失败");
        }
        return AjaxResult.success("新增服务中心成功");
    }

    /**
     * 获取全部公司列表
     *
     * @return
     */
    @GetMapping("/getCompanyAllList")
    public AjaxResult getCompanyAllList() {
        return AjaxResult.success("获取全部公司列表成功", retailCompanyService.getRetailCompanyAllList());
    }

    /**
     * 获取全部仓库列表
     *
     * @return
     */
    @GetMapping("/getWarehouseAllList")
    public AjaxResult getWarehouseAllList() {
        return AjaxResult.success("获取全部仓库列表成功", retailWarehouseService.getAllList());
    }

    /**
     * 获取全部路线列表
     *
     * @return
     */
    @GetMapping("/getRouteAllList")
    public AjaxResult getRouteAllList() {
        return AjaxResult.success("获取全部路线列表成功", retailRouteService.getAllList());
    }

    /**
     * 修改服务中心信息
     *
     * @param retailServiceCenter 服务中心参数
     * @return
     */
    @PutMapping
    public AjaxResult editRetailWarehouse(@Validated @RequestBody RetailServiceCenter retailServiceCenter) {
        RetailServiceCenterDetailVO retailServiceCenterOld = retailServiceCenterService.getById(retailServiceCenter.getId());
        // 判断服务中心名称是否存在
        RetailServiceCenter retailServiceCenterByName = retailServiceCenterService.getOne(new LambdaQueryWrapper<RetailServiceCenter>().eq(RetailServiceCenter::getName, retailServiceCenter.getName()));
        if (retailServiceCenterByName != null && !retailServiceCenterByName.getId().equals(retailServiceCenterOld.getId())) {
            return AjaxResult.error(HttpStatus.ERROR, "服务中心名称已存在");
        }
        // 判断服务中心编号是否存在
        RetailServiceCenter retailServiceCenterByNo = retailServiceCenterService.getOne(new LambdaQueryWrapper<RetailServiceCenter>().eq(RetailServiceCenter::getCode, retailServiceCenter.getCode()));
        if (retailServiceCenterByNo != null && !retailServiceCenterByNo.getId().equals(retailServiceCenterOld.getId())) {
            return AjaxResult.error(HttpStatus.ERROR, "服务中心编号已存在");
        }
        int resCount = retailServiceCenterService.editRetailServiceCenter(retailServiceCenter);
        if (resCount <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "服务中心信息修改失败");
        }
        return AjaxResult.success("修改服务中心信息成功");
    }

    /**
     * 根据id获取服务中心信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.success("获取服务中心信息成功", retailServiceCenterService.getById(id));
    }

    /**
     * 根据id删除服务中心信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id) {
        boolean isRemove = retailServiceCenterService.removeById(id);
        return isRemove ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    /**
     * 获取所有服务中心列表成功
     *
     * @return
     */
    @GetMapping("/getAllList")
    public AjaxResult getAllList() {
        return AjaxResult.success("获取所有服务中心列表成功", retailServiceCenterService.getAllList());
    }
}
